package com.ngkj.zmfptback.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.bean.RoleEntity;
import com.ngkj.zmfptback.sys.service.read.RoleReadService;
import com.ngkj.zmfptback.sys.service.write.RoleWriteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ChenHN
 * @since 2021/12/21 creation
 **/
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleReadService roleReadService;

    @Resource
    private RoleWriteService roleWriteService;

    @GetMapping("/getAllRole")
    public CommonReslut getAllRole(RoleEntity roleEntity){
        List<RoleEntity> allRole = roleReadService.getAllRole(roleEntity);
        return new CommonReslut(ResultEnum.ok,allRole);
    }

    @GetMapping("/getPageRole")
    public CommonReslut getPageRole(RoleEntity roleEntity,int pageNo,int pageSize){
        try {
            Page<RoleEntity> page = new Page<>(pageNo, pageSize);
            IPage<RoleEntity> pageRole = roleReadService.getPageRole(page, roleEntity);
            return new CommonReslut(ResultEnum.ok,pageRole);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @DeleteMapping("/deleteRole")
    public CommonReslut deleteRole(Integer id){
        try {
            roleWriteService.deleteRole(id);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/updateRole")
    public CommonReslut updateRole(@RequestBody RoleEntity roleEntity){
        try {
            roleWriteService.updateRole(roleEntity,roleEntity.getAuthStr(),roleEntity.getMenuStr() );
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/addRole")
    public CommonReslut addRole(@RequestBody RoleEntity roleEntity){
        try {
            roleWriteService.addRole(roleEntity,roleEntity.getAuthStr(),roleEntity.getMenuStr());
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @GetMapping("getAuthByRole")
    public CommonReslut getAuthByRole(Long id){
        try {
            List<String> authsById = roleReadService.getAuthsById(id, "OPER" );
            return new CommonReslut(ResultEnum.ok,authsById);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }


    @GetMapping("/getMenuByRole")
    public CommonReslut getMenuByRole(Long id){
        try {
            List<String> menuIds = roleReadService.getMenuById(id);
            return new CommonReslut(ResultEnum.ok,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }
}
